/**
 * form提交内容与json对象转换
 */

var jsonstr;

//提交状态：-1：提交失败；0：待提交；1：提交中；4：提交完成
var submitStatus = 4;//默认提交完成，可进行提交。
$(function() {
	//初始化遮挡层
	initShadeDiv();
	//页面加载执行ajax动作获取显示数据
	$("form").each(function(){
		//执行遮挡层动作
		execShadeAct();
		jsonstr = "{";
		var actionPlusParam = $(this).attr("action");
		var action;
		var plusParam;
		if (actionPlusParam.indexOf('?') >= 0) {
			action = actionPlusParam.substring(0, actionPlusParam
					.indexOf('?'));
			plusParam = actionPlusParam.substring(actionPlusParam
					.indexOf('?') + 1);
		} else {
			action = actionPlusParam;
			plusParam = "";
		}
		
		var serializeArray = $(this).serializeArray();
		
		for(var i = 0;i<serializeArray.length;i++){
			if (jsonstr != '{')
				jsonstr += ",";
			jsonstr +="\""+serializeArray[i].name+"\":\""+sqlEscape(serializeArray[i].value)+"\"";
		}
		var param = plusParam;
		var params = param.split("&");
		var len = params.length;
		for ( var i = 0; i < len; i++) {
			var oparam = params[i];
			var oparams = oparam.split("=");
			if (oparams.length == 2) {
				if (jsonstr != '{')
					jsonstr += ",";
				jsonstr += "\"" + oparams[0] + "\":\"" + oparams[1] + "\"";
			}
		}
		jsonstr += "}";
		var formId = $(this).attr("id");
		if(formId==undefined)return ;
		var formRltDiv = formId+"RltDiv";
		var formRltTab = formId+"RltTab";
		alert(('#'+formRltTab).length);
		var paramJSON = eval('(' + jsonstr + ')');
		var iniSize = paramJSON.iniSize;
		var incSize = paramJSON.incSize;
		var ic = paramJSON.ic;
		wd8.SCROLL.scroll($('#'+formRltDiv).get(0), $('#'+formRltTab).get(0),
				action, paramJSON,formId,iniSize,incSize,ic);
		setScrollDiv(formId);
	});
	//执行提交动作，执行ajax动作获取显示数据
	execSubmit = function(e) {
		if($("#errorDiv2").length>0){
			$("#errorDiv2").get(0).style.display="block";
		}
		/*if(submitStatus!=4){
			return false;
		}else{
			submitStatus = 0;
		}*/
		//执行遮挡层动作
		execShadeAct();
		e.preventDefault();
		var submitEvent = $(this).attr("onsubmit");
		if(submitEvent!=null&&submitEvent!=undefined){
			if (submitEvent.indexOf('false') >= 0)
				return ;
			
			var submitEvents = submitEvent.split(";");
			var methodName;
			var method;
			for ( var i = 0; i < submitEvents.length; i++) {
				if (submitEvents[i].indexOf("return") < 0)
					continue;
				var iTmp0 = submitEvents[i].replace("return", "");
				var iTmp1 = iTmp0.replace(/(^\s*)|(\s*$)/g, "");
				var iTmp2 = iTmp1.replace(";", "");
				methodName = iTmp2.substring(0, iTmp2.indexOf("("));
				method = iTmp2;
				try {
					if (typeof (eval(methodName)) == "function") {
						var methodContent =  eval(methodName).toString();
						var methodContent2 = methodContent.replace(new RegExp("alert","gm"), "//alert");
						var methodContent3 = methodContent2.substring(methodContent2.indexOf("{")+1);
						var methodContent4 = methodContent3.substring(0,methodContent3.length-1);
						var ajax_exec=new Function(methodContent4);
						if(!ajax_exec()&&ajax_exec()!=undefined){
							return ;
							break;
						}
					}
				} catch (e) {
					return ;
				}
			}
		}
		jsonstr = "{";
		var actionPlusParam = $(this).attr("action");
		var action;
		var plusParam;
		if (actionPlusParam.indexOf('?') >= 0) {
			action = actionPlusParam.substring(0, actionPlusParam
					.indexOf('?'));
			plusParam = actionPlusParam.substring(actionPlusParam
					.indexOf('?') + 1);
		} else {
			action = actionPlusParam;
			plusParam = "";
		}
		
		var serializeArray = $(this).serializeArray();
		
		for(var i = 0;i<serializeArray.length;i++){
			if (jsonstr != '{')
				jsonstr += ",";
			jsonstr +="\""+serializeArray[i].name+"\":\""+sqlEscape(serializeArray[i].value)+"\"";
		}
		var param = plusParam;
		var params = param.split("&");
		var len = params.length;
		for ( var i = 0; i < len; i++) {
			var oparam = params[i];
			var oparams = oparam.split("=");
			if (oparams.length == 2) {
				if (jsonstr != '{')
					jsonstr += ",";
				jsonstr += "\"" + oparams[0] + "\":\"" + oparams[1] + "\"";
			}
		}
		jsonstr += "}";
		var formId = $(this).attr("id");
		if(formId==undefined)return ;
		var formRltDiv = formId+"RltDiv";
		var formRltTab = formId+"RltTab";
		var paramJSON = eval('(' + jsonstr + ')');
		var iniSize = paramJSON.iniSize;
		var incSize = paramJSON.incSize;
		wd8.SCROLL.scroll($('#'+formRltDiv).get(0), $('#'+formRltTab).get(0),
				action, eval('(' + jsonstr + ')'),formId,iniSize,incSize);
		setScrollDiv(formId);
	};
	
	$("form").submit(execSubmit);
	
});
function setScrollDiv(formId){
	var scheight;
	var divheight;
	//获取高级查询项的高级及显示滚动数据的高度
	if($(".search_content").length>0){
		scheight = $(".search_content").height();
		divheight = $("#"+formId+"RltDiv").height();
	}
	if(!window.parent.selTermState){
		//如果不存在不需要折叠的查询条件，则不进行折叠
		if($('.nopucker').length<=0&&$("li .search_content").length>0){
			var search_contentheight =  $("li .search_content").height();
			$("li .search_content").slideUp("fast");
			$(".more").attr("src", "images/zh_CN/more.png");
			//$("#"+formId+"RltDiv").animate({height:divheight+scheight+20},"fast");
			//条数位置同时跟随变化
			if($('#ScrollDataCount').length>0){
				var OldScrollDataCountTop =  $('#ScrollDataCount').css('top');
				search_contentheight = parseFloat(search_contentheight,10);
				OldScrollDataCountTop = parseFloat(OldScrollDataCountTop,10);
				$('#ScrollDataCount').animate({top:OldScrollDataCountTop-search_contentheight-15},"fast");
			}
		}
	}
}
function shadeDiv(){
	if($(".more").length>0){
		var moreLeft = $(".more").offset().left;
		var moreTop = $(".more").offset().top;
		var moreHeight = $(".more").height();
		var moreWidth = $(".more").width();
		var moreMarkDiv$ = $("<div id='moreMarkDiv'>")
					.css("position","absolute")
					.css('left',moreLeft)
					.css('top',moreTop)
					.height(moreHeight)
					.width(moreWidth)
					.css('background-color','#CCC')
					.addClass('transparent_class')
					.appendTo($("body"))
					.hide();
		$(".more").ajaxStart(function(){
			moreMarkDiv$.show();
		});
		$(".more").ajaxSuccess(function(){
			moreMarkDiv$.hide();
		});
	}
}
/**
 * 初始化遮挡层
 * @return
 */
function initShadeDiv(){
	if($(".more").length>0){
		var moreLeft = $(".more").offset().left;
		var moreTop = $(".more").offset().top;
		var moreHeight = $(".more").height();
		var moreWidth = $(".more").width();
		var moreMarkDiv$ = $("<div id='moreMarkDiv'>")
					.css("position","absolute")
					.css('left',moreLeft)
					.css('top',moreTop)
					.height(moreHeight)
					.width(moreWidth)
					.css('background-color','#CCC')
					.addClass('shading')
					.appendTo($("body"))
					.hide();
	}
}
/**
 * 执行遮挡层动作
 * @return
 */
function execShadeAct(){
	if($('#moreMarkDiv').length>0){
		$('#moreMarkDiv').bind('ajaxStart',function(){
			$('#moreMarkDiv').show();
		});
		$('#moreMarkDiv').bind('ajaxSuccess',function(){
			$('#moreMarkDiv').fadeTo(1000,0.3).delay(1000).hide();
			$('#moreMarkDiv').unbind('ajaxStart');
			$('#moreMarkDiv').unbind('ajaxSuccess');
		});
	}
}

function sqlEscape(val){
	val = val.replace(/'/g,"''")
			 .replace(/%/g,"[%]");
	return val;
}